//
//  YKApproveBottomView.m
//  YunKang
//
//  Created by leoking870 on 2017/10/22.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKApproveBottomView.h"
#import "YKProcessTrackingViewController.h"
#import "YKTurnOverViewController.h"
#import "YKPopupView.h"
@interface YKApproveBottomView ()
@property (nonatomic, strong) UIButton* processTrackButton;
@property (nonatomic, strong) UIButton* agreeButton;
@property (nonatomic, strong) UIButton* disagreeButton;
@end

@implementation YKApproveBottomView

- (instancetype)init
{
    self = [super init];
    if (self) {
        UIButton* processTrackButton = [[UIButton alloc]init];
        [self addSubview:processTrackButton];
        UIButton *agreeButton = [[UIButton alloc]init];
        [self addSubview:agreeButton];
        UIButton *disagreeButton = [[UIButton alloc]init];
        [self addSubview:disagreeButton];
        [processTrackButton setTitle:@"流程跟踪" forState:UIControlStateNormal];
        [agreeButton setTitle:@"同意" forState:UIControlStateNormal];
        [disagreeButton setTitle:@"不同意" forState:UIControlStateNormal];


        self.processTrackButton = processTrackButton;
        self.agreeButton = agreeButton;
        self.disagreeButton = disagreeButton;

        [disagreeButton setBackgroundImage:TFSTYLE(buttonEnableBackgroundImage) forState:UIControlStateNormal];//
        [disagreeButton setBackgroundImage:[UIImage tf_imageWithColor:[UIColor grayColor]] forState:UIControlStateDisabled];//

        [processTrackButton setBackgroundImage:TFSTYLE(buttonEnableBackgroundImage) forState:UIControlStateNormal];//
        [processTrackButton setBackgroundImage:[UIImage tf_imageWithColor:[UIColor grayColor]] forState:UIControlStateDisabled];//

        [agreeButton setBackgroundImage:TFSTYLE(buttonEnableBackgroundImage) forState:UIControlStateNormal];//
        [agreeButton setBackgroundImage:[UIImage tf_imageWithColor:[UIColor grayColor]] forState:UIControlStateDisabled];//


        [processTrackButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
        [agreeButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
        [disagreeButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
        processTrackButton.titleLabel.font = [UIFont systemFontOfSize:14];
        agreeButton.titleLabel.font = [UIFont systemFontOfSize:14];
        disagreeButton.titleLabel.font = [UIFont systemFontOfSize:14];
        
        [disagreeButton addTarget:self action:@selector(disagree:) forControlEvents:UIControlEventTouchUpInside];
        [processTrackButton addTarget:self action:@selector(processTrack:) forControlEvents:UIControlEventTouchUpInside];
        [agreeButton addTarget:self action:@selector(agree:) forControlEvents:UIControlEventTouchUpInside];
        
        [@[processTrackButton,agreeButton,disagreeButton] mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:1 leadSpacing:0 tailSpacing:0];
        [@[processTrackButton,agreeButton,disagreeButton] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top);
            make.bottom.equalTo(self.mas_bottom);
            make.height.mas_equalTo(44);
        }];
    }
    return self;
}

- (void)disagree:(id)sender {
    if (self.handler) {
        self.handler(2);
    }
}

- (void)processTrack:(id)sender {
    if (self.handler) {
        self.handler(0);
    }
}

- (void)agree:(id)sender {
    if (self.handler) {
        self.handler(1);
    }
}

+ (void)addSelfToView:(UIViewController *)viewController ID:(NSString *)ID  approveStatus:(NSInteger)status{
    YKApproveBottomView *bottom = [[YKApproveBottomView alloc]init];
    if(status == 2){
        bottom.agreeButton.enabled = NO;
        bottom.disagreeButton.enabled = NO;
//        bottom.processTrackButton.enabled = NO;
    }
    bottom.handler = ^(NSUInteger index) {
        switch (index) {
            case 0:
                //流程跟踪
            {
                if (ID) {
                    YKProcessTrackingViewController *vc = [YKProcessTrackingViewController new];
                    vc.m_id = ID;
                    [YKPopupView showPopupViewWithViewController:vc];
                }
                else {
                    [viewController showHint:@"此工作流不存在,请返回列表刷新重试"];
                }
            }
                break;
            case 1:
                //同意
            {
                YKTurnOverViewController *vc = [YKTurnOverViewController new];
                vc.m_id = ID;
                vc.type = YKTurnOverTypeApprove;
                vc.submitBlock = ^(NSString *text, NSString *businessId, NSString *nextId) {
                    if (businessId) {
                        [TFHTTPMANAER POST:@"workExamine/completeWorkExamine.htm" parameters:@{@"mark":text,@"businessId":businessId,@"nextId":nextId?:@""} HUD:@"同意中..." success:^(id responseObject) {
                            [viewController showHint:@"同意成功"];
                            [viewController.navigationController popViewControllerAnimated:YES];
                        } failure:^(id responseObject, NSError *error) {
                            [viewController showHint:@"同意失败, 请点击重试"];
                        }];
                    }
                    else {
                        [viewController showHint:@"此工作流不存在,请返回列表刷新重试"];
                    }
                };
                
                [YKPopupView showPopupViewWithViewController:vc];
            }
                break;
            case 2:
                //不同意
            {
                YKTurnOverViewController *vc = [YKTurnOverViewController new];
                vc.m_id = ID;
                vc.type = YKTurnOverTypeDisapprove;
                vc.submitBlock = ^(NSString *text, NSString *businessId, NSString *nextId) {
                    if (businessId) {
                        [TFHTTPMANAER POST:@"workExamine/returnWorkExamine.htm" parameters:@{@"mark":text,@"businessId":businessId,@"perTask":nextId?:@""} HUD:@"退回中..." success:^(id responseObject) {
                            [viewController showHint:@"退回成功"];
                            [viewController.navigationController popViewControllerAnimated:YES];
                        } failure:^(id responseObject, NSError *error) {
                            [viewController showHint:@"退回失败, 请点击重试"];
                        }];
                    }
                    else {
                        [viewController showHint:@"此工作流不存在,请返回列表刷新重试"];
                    }
                };
                
                [YKPopupView showPopupViewWithViewController:vc];
            }
                break;
            default:
                break;
        }
    };
    
    [viewController.view addSubview:bottom];
    [bottom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.equalTo(viewController.view);
    }];
}


@end
